package com.freesky.quartz.task;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;

import com.freesky.biz.icbc.domain.TFinanceAccount;
import com.freesky.biz.icbc.mapper.FinanceReferrerMapper;
import com.freesky.biz.icbc.service.IcbcLifeService;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 工行推荐礼-养老金推荐
 */
@Component("financeAccountTask")
public class FinanceAccoutTask {

    private static final Logger logger = LoggerFactory.getLogger(FinanceAccoutTask.class);

    @Autowired
    private FinanceReferrerMapper financeReferrerMapper;

    @Autowired
    private IcbcLifeService icbcLifeService;


    /**
     * 工行推荐礼-养老金推荐处理逻辑
     */
    public void taskProcess() {
        logger.info("financeAccountTask task start");
        List<TFinanceAccount> financeAccountList = financeReferrerMapper.selectUnProcessList();
        for(TFinanceAccount financeAccount : financeAccountList){
            String nextProcessDate = DateUtil.format(DateUtil.offsetHour(new Date(),12),"yyyy-MM-dd HH:mm:ss");

            String status = financeAccount.getStatus();
            long nDays = DateUtil.between(new Date(),DateUtil.parse(financeAccount.getCreateDate()),
                    DateUnit.DAY,false);
            if(nDays>=14){
                status = "2";
                financeReferrerMapper.updateFinaceAccountStatus(financeAccount.getId(),status,
                        nextProcessDate,"过期","");
                continue;
            }

            if(StringUtils.isEmpty(financeAccount.getUserId())){
                AjaxResult custmResult = icbcLifeService.wjyxCustmIdentifyQry(financeAccount.getMobile(),
                        financeAccount.getIdno());
                if(!custmResult.isSuccess()){
                    financeReferrerMapper.updateFinaceAccountStatus(financeAccount.getId(),status,
                            nextProcessDate,"","");
                    continue;
                }

                Map<String,String> respMap = (Map<String,String>) custmResult.get("data");
                if(respMap.get("retcode").equalsIgnoreCase("00")) {
                    financeAccount.setUserId(respMap.get("userId"));
                } else {
                    financeReferrerMapper.updateFinaceAccountStatus(financeAccount.getId(),status,
                            nextProcessDate,respMap.get("retmsg"),"");
                    continue;
                }
            } //END IF

            AjaxResult equityResult = icbcLifeService.wjyxEquityQry(financeAccount.getUserId(),
                    "ylyl0001","20250114","",financeAccount.getMobile());
            if(!equityResult.isSuccess()){
                financeReferrerMapper.updateFinaceAccountStatus(financeAccount.getId(),status,
                        nextProcessDate,equityResult.getMsg(),financeAccount.getUserId());
                continue;
            }

            Map<String,String> respMap = (Map<String,String>) equityResult.get("data");
            if(!respMap.get("retcode").equalsIgnoreCase("00")){
                financeReferrerMapper.updateFinaceAccountStatus(financeAccount.getId(),status,
                        nextProcessDate,respMap.get("retmsg"),financeAccount.getUserId());
                continue;
            }

            String ylflag = respMap.get("ylflag");
            String openTime = respMap.get("openTime");

            if(StringUtils.isEmpty(ylflag)){
                financeReferrerMapper.updateFinaceAccountStatus(financeAccount.getId(),status,
                        nextProcessDate,respMap.get("retmsg"),financeAccount.getUserId());
                continue;
            }

            if("Y10".equalsIgnoreCase(ylflag)){
                status = "1";
                financeReferrerMapper.updateFinaceAccountStatus(financeAccount.getId(),status,
                        nextProcessDate,"成功"+ylflag +","+openTime,financeAccount.getUserId());
                //增加30元
                financeReferrerMapper.updateReferrerAmt(3000,financeAccount.getReferrerId());
            } else if("Y00".equalsIgnoreCase(ylflag)){
                financeReferrerMapper.updateFinaceAccountStatus(financeAccount.getId(),status,
                        nextProcessDate,ylflag +","+openTime,financeAccount.getUserId());
            } else {
                status = "3";
                financeReferrerMapper.updateFinaceAccountStatus(financeAccount.getId(),status,
                        nextProcessDate,ylflag +","+openTime,financeAccount.getUserId());
            }

//            else if(ylflag.indexOf("Y101")>=0){       //开通，黑名单
//                status = "3";
//                financeReferrerMapper.updateFinaceAccountStatus(financeAccount.getId(),status,
//                        nextProcessDate,ylflag +","+openTime,financeAccount.getUserId());
//
//            } else if(ylflag.indexOf("Y000")>=0) {      //未开通
//                financeReferrerMapper.updateFinaceAccountStatus(financeAccount.getId(),status,
//                        nextProcessDate,ylflag +","+openTime,financeAccount.getUserId());
//            } else {
//                status = "3";
//                financeReferrerMapper.updateFinaceAccountStatus(financeAccount.getId(),status,
//                        nextProcessDate,ylflag +","+openTime,financeAccount.getUserId());
//            }
        }
    }
}
